package com.freesky.biz.activity.controller;

import cn.hutool.core.convert.Convert;
import com.freesky.biz.activity.domain.TAlipayBill;
import com.freesky.biz.activity.domain.TAlipayRefundBill;
import com.freesky.biz.activity.domain.TCouponBill;
import com.freesky.biz.activity.service.IAlipayBillTasklService;
import com.freesky.biz.activity.service.ICouponBillService;
import com.freesky.common.annotation.Log;
import com.freesky.common.core.controller.BaseController;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.page.TableDataInfo;
import com.freesky.common.enums.BusinessType;
import com.freesky.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/activity/alipay/bill")
public class AlipayBillTaskController extends BaseController {

    @Autowired
    private IAlipayBillTasklService alipayBillTasklService;

    /**
     * 支付宝核销明细
     */
    @PreAuthorize("@ss.hasPermi('activity:alipaybill:list')")
    @GetMapping("/list")
    public TableDataInfo list(TAlipayBill entity) {
        startPage();
        if(StringUtils.isNotEmpty(entity.getStockId())){
            String stockId = entity.getStockId().trim();
            stockId = Convert.toDBC(stockId);
            stockId = stockId.replaceAll("\r\n",",");
            stockId = stockId.replaceAll("\\n",",");
            entity.setStockId(stockId);
        }
        List<TAlipayBill> list = alipayBillTasklService.selectBillList(entity);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('activity:alipaybill:list')")
    @GetMapping("/summary")
    public AjaxResult summary(TAlipayBill entity) {
        if(StringUtils.isNotEmpty(entity.getStockId())){
            String stockId = entity.getStockId().trim();
            stockId = Convert.toDBC(stockId);
            stockId = stockId.replaceAll("\r\n",",");
            stockId = stockId.replaceAll("\\n",",");
            entity.setStockId(stockId);
        }
        return AjaxResult.success(alipayBillTasklService.selectBillSummaryData(entity));
    }

    @Log(title = "支付宝核销明细", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('activity:alipaybill:export')")
    @GetMapping("/export")
    public AjaxResult export(TAlipayBill entity,
                             HttpServletResponse response) {
        if(StringUtils.isNotEmpty(entity.getStockId())){
            String stockId = entity.getStockId().trim();
            stockId = Convert.toDBC(stockId);
            stockId = stockId.replaceAll("\r\n",",");
            stockId = stockId.replaceAll("\\n",",");
            entity.setStockId(stockId);
        }
        return alipayBillTasklService.exportFile(entity);
    }

    @Log(title = "支付宝核销明细汇总", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('activity:alipaybill:export')")
    @GetMapping("/exportSummary")
    public AjaxResult exportSummary(TAlipayBill entity,
                                    HttpServletResponse response) {
        if(StringUtils.isNotEmpty(entity.getStockId())){
            String stockId = entity.getStockId().trim();
            stockId = Convert.toDBC(stockId);
            stockId = stockId.replaceAll("\r\n",",");
            stockId = stockId.replaceAll("\\n",",");
            entity.setStockId(stockId);
        }
        return alipayBillTasklService.exportSummaryFile(entity);
    }

    /**
     * 支付宝核销明细
     */
    @PreAuthorize("@ss.hasPermi('activity:alipayrefundbill:list')")
    @GetMapping("/refundList")
    public TableDataInfo refundList(TAlipayRefundBill entity) {
        startPage();
        if(StringUtils.isNotEmpty(entity.getStockId())){
            String stockId = entity.getStockId().trim();
            stockId = Convert.toDBC(stockId);
            stockId = stockId.replaceAll("\r\n",",");
            stockId = stockId.replaceAll("\\n",",");
            entity.setStockId(stockId);

        }
        List<TAlipayRefundBill> list = alipayBillTasklService.selectRefundBillList(entity);
        return getDataTable(list);
    }


    @PreAuthorize("@ss.hasPermi('activity:alipayrefundbill:list')")
    @GetMapping("/refundSummary")
    public AjaxResult refundSummary(TAlipayRefundBill entity) {
        if(StringUtils.isNotEmpty(entity.getStockId())){
            String stockId = entity.getStockId().trim();
            stockId = Convert.toDBC(stockId);
            stockId = stockId.replaceAll("\r\n",",");
            stockId = stockId.replaceAll("\\n",",");
            entity.setStockId(stockId);

        }
        return AjaxResult.success(alipayBillTasklService.selectRefundBillSummaryData(entity));
    }


    @Log(title = "支付宝退款明细", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('activity:alipayrefundbill:export')")
    @GetMapping("/exportRefund")
    public AjaxResult exportRefund(TAlipayRefundBill entity,
                             HttpServletResponse response) {
        if(StringUtils.isNotEmpty(entity.getStockId())){
            String stockId = entity.getStockId().trim();
            stockId = Convert.toDBC(stockId);
            stockId = stockId.replaceAll("\r\n",",");
            stockId = stockId.replaceAll("\\n",",");
            entity.setStockId(stockId);
        }
        return alipayBillTasklService.exportRefundFile(entity);
    }

}
